﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace EvoWars2.Stuffs
{
    class CollisionResponce2D
    {


        public Vector2 GetResponceVector(Rectangle dynamicObject, Rectangle staticObject)
        {
            Vector2 responce = new Vector2();

            decimal halfWidth1 = dynamicObject.Width / 2.0m;
            decimal halfHeight1 = dynamicObject.Height / 2.0m;

            decimal halfWidth2 = staticObject.Width / 2.0m;
            decimal halfHeight2 = staticObject.Height / 2.0m;


            decimal xDisplacement = halfWidth1 + halfWidth2 - Math.Abs(staticObject.Center.X - dynamicObject.Center.X);

            decimal yDisplacement = halfHeight1 + halfHeight2 - Math.Abs(staticObject.Center.Y - dynamicObject.Center.Y);

            if (Math.Abs(xDisplacement) < Math.Abs(yDisplacement))
            {
                Vector2 dynObjVector = new Vector2(dynamicObject.Center.X, 0);
                Vector2 statObjVector = new Vector2(staticObject.Center.X, 0);
                Vector2 direction = dynObjVector - statObjVector;
                direction.Normalize();
                direction = direction * (float)xDisplacement;
                responce = direction;
                //responce = new Vector2((float)xDisplacement, 0f);
            }
            else
            {
                Vector2 dynObjVector = new Vector2(0, dynamicObject.Center.Y);
                Vector2 statObjVector = new Vector2(0, staticObject.Center.Y);
                Vector2 direction = dynObjVector - statObjVector;
                direction.Normalize();
                direction = direction * (float)yDisplacement;
                responce = direction;




                //responce = new Vector2(0f, (float)yDisplacement);
            }




            return responce;
        }

        /*
        public Vector2 GetCorrectionVector(Rectangle dynamicObject, Rectangle staticObject)
        {
            Vector2 ret = new Vector2();

            float x1 = Math.Abs(dynamicObject.Right - staticObject.Left);
            float x2 = Math.Abs(dynamicObject.Left - staticObject.Right);
            float y1 = Math.Abs(dynamicObject.Bottom - staticObject.Top);
            float y2 = Math.Abs(dynamicObject.Top - staticObject.Bottom);

            // calculate displacement along X-axis
            if (x1 < x2)
            {
                ret.X = -x1;
                //ret.DirectionX = DirectionX.Left;
            }
            else if (x1 > x2)
            {
                ret.X = x2;
                //ret.DirectionX = DirectionX.Right;
            }

            // calculate displacement along Y-axis
            if (y1 < y2)
            {
                ret.Y = -y1;
                //ret.DirectionY = DirectionY.Up;
            }
            else if (y1 > y2)
            {
                ret.Y = y2;
                //ret.DirectionY = DirectionY.Down;
            }

            return ret;
        }
        */

    }
}
